package org.ediba.siri.web.rest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.ediba.siri.core.image.domain.ImageKeyword;
import org.ediba.siri.core.image.service.ImageService;
import org.json.JSONException;
import org.springframework.web.bind.annotation.ResponseBody;

public class ImageKeywordController extends Controller {
	
	private ImageService imageService;
	public void setImageService(ImageService imageService) {this.imageService = imageService;}
	
	@ResponseBody
	public Map<String,Object> postHandler(HttpServletRequest request, HttpServletResponse response, ImageKeywordCommand command){
		
		String word = null;
		
		if (command.getData() != null)
			try {word = command.getData().getString("word");
			} catch (JSONException e) {e.printStackTrace();}
		else if (command.getWord() != null && !command.getWord().isEmpty())
			word = command.getWord();
		
		ImageKeyword imageKeyword = this.imageService.keywordCreate(word);

		Map<String,Object> _result = new HashMap<String,Object>(2);
		_result.put("success", word != null);
		_result.put("data", imageKeyword);
		return _result;
	}
	
	@ResponseBody
	public Map<String,Object> getHandler(HttpServletRequest request, HttpServletResponse response, ImageKeywordCommand command){
		List<ImageKeyword> imageKeywords = null;
		Integer total = null;
		
		if (command.getId() != null){
			Map<String,Object> _result = new HashMap<String,Object>(2);
			_result.put("data", this.imageService.keywordRetrieve(command.getId()));
			_result.put("success", true);
			return _result;
		}
		else if (command.getWord() != null&&!command.getWord().isEmpty())
			imageKeywords = this.imageService.keywordRetrieve(command.getWord(),command.getStart(),(command.getStart() != null) ? command.getLimit() : 0);
		else if (command.getIdsImage() != null)
			imageKeywords = this.imageService.keywordRetrieveByImage(command.getIdsImage()[0]);
		else if (command.getIdsImageKeyword() != null)
			imageKeywords = this.imageService.keywordRetrieveByKeyword(command.getIdsImageKeyword()[0]);
		else {
			imageKeywords = this.imageService.keywordRetrieve(command.getSort(),command.getSort() != null ? command.getDir() : true, command.getStart(), (command.getStart() != null) ? command.getLimit() : 0);
			total = this.imageService.keywordRetrieve(null, false, null, 0).size();
		}
		
		if (total == null)
			total = imageKeywords.size();
		
		Map<String,Object> _result = new HashMap<String,Object>(4);
		_result.put("data", imageKeywords);
		_result.put("total", total);
		_result.put("success", true);
		_result.put("message", "OK");
		return _result;
	}
	
	@ResponseBody
	public Map<String,Object> putHandler(HttpServletRequest request, HttpServletResponse response, ImageKeywordCommand command){
		
		Integer id = null;
		String word = null;
		Integer[] idsImagekeyword = {};
		Boolean add = false;
		
		if (command.getData()!=null)
			try {
				id = command.getData().getInt("id");
				word = command.getData().getString("word");
				
			} catch (JSONException e) {e.printStackTrace();}
		else{
			id = command.getId();
			word = command.getWord();
			idsImagekeyword = command.getIdsImageKeyword();
			add = command.getAdd();
		}
				
		ImageKeyword imageKeyword = this.imageService.keywordRetrieve(id);
		if (word != null) 
			imageKeyword.setWord(word);
		
		if (add!=null){
			Set<ImageKeyword> keywordFamilyB = imageKeyword.getImagekeywordsForIdImageKeywordB();
			if (add){
				for (Integer idImageKeyword : idsImagekeyword) {
					keywordFamilyB.add(this.imageService.keywordRetrieve(idImageKeyword));
					ImageKeyword ikB = this.imageService.keywordRetrieve(idImageKeyword);
					ikB.getImagekeywordsForIdImageKeywordB().add(imageKeyword);
				}
			}else{
				for (Integer idImageKeyword : idsImagekeyword) {
					keywordFamilyB.remove(this.imageService.keywordRetrieve(idImageKeyword));
					ImageKeyword ikB = this.imageService.keywordRetrieve(idImageKeyword);
					ikB.getImagekeywordsForIdImageKeywordB().remove(imageKeyword);
				}
			}
		}
		imageKeyword = this.imageService.keywordUpdate(imageKeyword);
			
		Map<String,Object> _result = new HashMap<String,Object>(2);
		_result.put("success", true);
		_result.put("data", imageKeyword);
		return _result;
	}
	
	@ResponseBody
	public Map<String,Object> deleteHandler(HttpServletRequest request, HttpServletResponse response, ImageKeywordCommand command){
		
		Integer id = null;
		try {id = command.getData().getInt("id");
		} catch (JSONException e) {e.printStackTrace();}
		
		boolean success = this.imageService.keywordDelete(id);
		
		Map<String,Object> _result = new HashMap<String,Object>(1);
		_result.put("success", success);
		return _result;
	}

}
